
var realname;
var orgId;
var userId;

$(function() {
	var offWidth = document.body.clientWidth
	// 设计稿1rem = 100px
	document.getElementsByTagName("html")[0].style.fontSize = offWidth*(100/1920) + 'px'; //把rem的值复制给顶级标签html的font-size
	console.log('offWidth:'+offWidth)

	window.onresize = function(){
	    location.reload()
	}
	
	realname = GetQueryString("userName");
	orgId = GetQueryString("orgId");
	userId = GetQueryString("userId");
	$('#userName').val(GetQueryString("userName"));
	
    validateRule();
    
    validateAccountRule();
    
    $(".i-checks").iCheck({checkboxClass:"icheckbox_square-green-login"});
	$('.imgcode').click(function() {
		var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
		$(".imgcode").attr("src", url);
	});
    
});

//模态框
let hideView = function () {
    document.querySelector('.bg-content').style.display = 'none'
    document.querySelector('.login-content').style.display = 'none'
}
let showView = function () {
    document.querySelector('.bg-content').style.display = 'block'
    document.querySelector('.login-content').style.display = 'block'
}

function GetQueryString(name)
{
	 var urldecode = decodeURI(window.location.search);
	 //alert(urldecode.substr(1));
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = urldecode.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
     
     if(r!=null)return  unescape(r[2]); return null;
}

/*$.validator.setDefaults({
    submitHandler: function() {
		login();
    }
});*/

/*var validator = $("#accountBindSubmit").validate({
    submitHandler:function(){
    	accountBindLogin();
    }    
});*/

function login() {
	$.modal.loading($("#setaccountSubmit").data("loading"));
	var username = $.common.trim($("input[name='accountNum']").val());
    var password = $.common.trim($("input[name='accountPwd']").val());
    $.ajax({
        type: "post",
        url: ctx + "oaPCRegister",
        data: {
        	"userName": realname,
        	"orgId": orgId,
        	"oaUserId": userId,
            "loginName": username,
            "password": password
        },
        success: function(r) {
            if (r.code == 0) {
            	location.href = ctx + 'index';
            } else {
            	$.modal.closeLoading();
            	$('.imgcode').click();
            	$.modal.msg(r.msg);
            }
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#setaccountForm").validate({
        rules: {
        	accountNum: {
                required: true
            },
        	accountPwd: {
                required: true
            }
        },
        messages: {
        	accountNum: {
                required: "",
            },
        	accountPwd: {
                required: "",
            }
        },
        submitHandler: function(form){   //表单提交句柄,为一回调函数，带一个参数：form   
           // alert("提交表单111");   
        	login();
        }
    })
}

function accountBindLogin(){
	$.modal.loading($("#accountBindSubmit").data("loading"));
	var loginName = $.common.trim($("input[name='loginName']").val());
    var loginPwd = $.common.trim($("input[name='loginPwd']").val());
    $.ajax({
        type: "post",
        url: ctx + "oaAccountPCBind",
        data: {
        	"orgId": orgId,
        	"oaUserId": userId,
        	"userName": realname,
            "loginName": loginName,
            "loginPwd": loginPwd
        },
        success: function(r) {
            if (r.code == 0) {
            	location.href = ctx + 'index';
            } else {
            	$.modal.closeLoading();
            	$('.imgcode').click();
            	$.modal.msg(r.msg);
            }
        }
    });
}

function validateAccountRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    	$("#accountBindForm").validate({
	        rules: {
	        	loginName: {
	                required: true
	            },
	            loginPwd: {
	                required: true
	            }
	        },
	        messages: {
	        	loginName: {
	                required: "",
	            },
	            loginPwd: {
	                required: "",
	            }
	        },
	        submitHandler: function(form){   //表单提交句柄,为一回调函数，带一个参数：form   
	           // alert("提交表单222");   
	        	accountBindLogin();
	        }   
    })
}

